﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyNhaSach.BusinessLogicLayer
{
    class QuanLyKhachHangBLL
    {
       
        private int DemChuSo(int number)
        {
            return number < 10 ? 1 : 1 + DemChuSo(number / 10);
        }
        public DataTable LayDanhSach()
        {
            DataAccessLayer.KhachHangDAO a = new DataAccessLayer.KhachHangDAO();
            return a.LayDanhSach();
        }

        public void ThemKhachHang(string makh, string tenkh, string diachi, string sdt)
        {
             DataAccessLayer.KhachHangDAO a = new DataAccessLayer.KhachHangDAO();
             a.ThemKhachHang(makh, tenkh, diachi, sdt);
        }
        public DataTable LayTTKH(string makh)
        { 
            DataAccessLayer.KhachHangDAO a = new DataAccessLayer.KhachHangDAO();
            DataTable data = a.KiemTraTonTai(makh);
            if (data.Rows.Count == 1)
            {
                return a.LayTTKH(makh);
            }
            else
            {
                MessageBox.Show("Khach hang khong ton tai trong he thong, vui long them khach hang!");
                return null;
            }
        }

        public AutoCompleteStringCollection TaoAutocompleteTen()
        {
            DataAccessLayer.KhachHangDAO a = new DataAccessLayer.KhachHangDAO();
            DataTable data = a.LayDanhSach();
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            foreach (DataRow x in data.Rows)
            {
                auto.Add(x["TenKH"].ToString());
            }
            return auto;
        }
        public AutoCompleteStringCollection TaoAutocompleteMa()
        {
            DataAccessLayer.KhachHangDAO k = new DataAccessLayer.KhachHangDAO();
            DataTable data = k.LayDanhSach();
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            foreach (DataRow x in data.Rows)
            {
                auto.Add(x["MaKH"].ToString());
            }
            return auto;
        }
        public string TaoMaKhachHang()
        {
            string result = "K";
            DataAccessLayer.KhachHangDAO d = new DataAccessLayer.KhachHangDAO();
            DataTable data = d.LayDanhSach();
            int count = data.Rows.Count + 1;
            if (count >= 9999) throw new Exception("Het khong gian ma khach hang");
            int c = DemChuSo(count);
            for (int i = 0; i < (4 - c); i++)
            {
                result += "0";
            }
            result += count.ToString();
            return result;
        }
        public void ThayDoiTTKH(string makh, string tenkh, string diachi, string sdt)
        {
            DataAccessLayer.KhachHangDAO a = new DataAccessLayer.KhachHangDAO();
            a.ThayDoiTTKH(makh, tenkh, diachi, sdt);
        }
        public DataTable TimKiemTen(string key)
        {
            DataAccessLayer.KhachHangDAO a = new DataAccessLayer.KhachHangDAO();
            return a.TimKiemTen(key);
        }
        public DataTable TimKiemMaKH(string key)
        {
            DataAccessLayer.KhachHangDAO a = new DataAccessLayer.KhachHangDAO();
            return a.TimKiemMaKH(key);
        }
    }
}
